﻿
namespace EasyNet.Solr.Impl
{
    /// <summary>
    /// Solr转义字符过滤器，用于查询中需要包含特殊字符
    /// </summary>
    public class EscapingFilter : IQueryFilter<string>
    {
        private static readonly string[] specialChars = new string[] { "\\", "+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "*", "?", ":" };

        /// <summary>
        /// 过滤
        /// </summary>
        /// <param name="query">查询字符串</param>
        /// <returns>过滤后的查询字符串</returns>
        public string Filter(string query)
        {
            if (string.IsNullOrEmpty(query))
            {
                return query;
            }

            foreach (string str in specialChars)
            {
                query = query.Replace(str, @"\" + str);
            }

            return query;
        }
    }
}
